package com.joinway.platform.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.joinway.admin.bean.view.Ddl2CodeColumn;
import com.joinway.platform.bean.AdminPageItem;
import com.joinway.platform.bean.ColumnField;
import com.joinway.platform.bean.RadioItem;

@Deprecated
public final class AutoCodeHelper {

	final static Map<String, String> javaTypeMap = new HashMap<>();
	final static Map<String, String> categoryMap = new HashMap<>();
	
	static{
		javaTypeMap.put("int", "Integer");
		
		categoryMap.put("String", "String");
		categoryMap.put("char", "String");
		categoryMap.put("Character", "String");
		categoryMap.put("boolean", "String");
		categoryMap.put("Boolean", "String");
		categoryMap.put("byte", "String");
		categoryMap.put("Byte", "String");
		categoryMap.put("Date", "Date");
		categoryMap.put("long", "Number");
		categoryMap.put("Long", "Number");
		categoryMap.put("int", "Number");
		categoryMap.put("Integer", "Number");
		categoryMap.put("short", "Number");
		categoryMap.put("Short", "Number");
		categoryMap.put("BigInteger", "Number");
		categoryMap.put("float", "Decimal");
		categoryMap.put("Float", "Decimal");
		categoryMap.put("double", "Decimal");
		categoryMap.put("Double", "Decimal");
		categoryMap.put("BigDecimal", "Decimal");
	}
	
	public static String getJavaType(ColumnField field){
		return javaTypeMap.containsKey(field.getType()) ? javaTypeMap.get(field.getType()) : field.getType();
	}
	
	public static String getCategory(ColumnField field){
		return categoryMap.containsKey(field.getType()) ? categoryMap.get(field.getType()) : field.getType();
	}

	public static String getCategory(ColumnField field, List<AdminPageItem> pages){
		boolean found = false;
		String category = "";
		
		for(AdminPageItem page : pages){
			if(field.getColumn().equalsIgnoreCase(page.getColumn())){
				found = true;
				category = page.getCategory();
			}
		}
		
		if(!found || StringUtils.isBlank(category)){
			category = categoryMap.containsKey(field.getType()) ? categoryMap.get(field.getType()) : field.getType();
		}
		
		return category;
	}
	
	public static boolean matched(ColumnField field, List<AdminPageItem> pages){
		return pages.stream().filter(p -> field.getColumn().equalsIgnoreCase(p.getColumn())).count() > 0;
	}
	
//	public static boolean isQuery(ColumnField field, List<AdminPage> pages){
//		return pages != null && pages.stream()
//				.filter(p -> p.isQuery() && field.getColumn().equalsIgnoreCase(p.getColumn()))
//				.count() > 0;
//	}
	
//	public static boolean isRequired(ColumnField field, List<AdminPage> pages){
//		return pages != null && pages.stream()
//				.filter(p -> p.isRequired() && field.getColumn().equalsIgnoreCase(p.getColumn()))
//				.count() > 0;
//	}
	
//	public static String getText(ColumnField field, List<AdminPage> pages){
//		String text = "";
//		
//		Optional<AdminPage> page = pages.stream().filter(p -> field.getColumn().equalsIgnoreCase(p.getColumn())).findFirst();
//		
//		if(page.isPresent()){
//			text = page.get().getText();
//		}
//		
//		return StringUtils.isBlank(text) ? field.getColumn() : text;
//	}
	
//	public static boolean getEditable(ColumnField field, List<AdminPage> pages){
//		boolean editable = true;
//		
//		Optional<AdminPage> page = pages.stream().filter(p -> field.getColumn().equalsIgnoreCase(p.getColumn())).findFirst();
//		
//		if(page.isPresent()){
//			editable = page.get().isEditable();
//		}
//		
//		return editable;
//	}
	
	public static void initDdl2CodeColumn(Ddl2CodeColumn column, ColumnField field, List<AdminPageItem> pages){
		String javaType = javaTypeMap.containsKey(field.getType()) ? javaTypeMap.get(field.getType()) : field.getType();
		column.setType(javaType);
		
		if(CollectionUtils.isNotEmpty(pages)){
			for(AdminPageItem page : pages){
				if(field.getColumn().equalsIgnoreCase(page.getColumn())){
					column.setQuery(page.isQuery());
					column.setRequired(page.isRequired());
					column.setText(StringUtils.isBlank(page.getText()) ? field.getColumn() : page.getText());
					column.setEditable(page.isEditable());
					column.setMin(StringUtils.trimToNull(page.getMin()));
					column.setMax(StringUtils.trimToNull(page.getMax()));
					column.setPrecision(StringUtils.trimToNull(page.getPrecision()));
					column.setVisible(page.isVisible());
					
					String category = page.getCategory();
					if(StringUtils.isBlank(category)){
						category = categoryMap.containsKey(field.getType()) ? categoryMap.get(field.getType()) : field.getType();
					}
					column.setCategory(category);
					
					if(StringUtils.isNotBlank(page.getList())){
						String[] items = page.getList().trim().split("\\s+");
						List<RadioItem> list = new ArrayList<>();
						
						for(int i = 0; i < items.length; i++){
							String[] parts = items[i].split(":|：|-");
							String value = parts[0];
							String text = parts.length > 1 ? parts[1] : value;
							RadioItem radio = new RadioItem(text, value);
							
//							if(i == 0){
//								radio.setChecked("checked");
//							}
							
							list.add(radio);
						}
						
						column.setList(list);
					}
				}
			}
		}
	}
	
	private AutoCodeHelper(){}
	
}
